package Bean.Log;

import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import Bean.UserinfoEntity;
import com.sun.istack.NotNull;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "OrderLog", schema = "bookloansystem", catalog = "bookloansystem")
public class OrderLog {
    int id;
    int orderid;
    Timestamp timestamp;
    String logdata;


    public OrderLog(){

    }
    public static boolean addLog(int orderid,String logdata){
        OrderLog orderLog=new OrderLog();
        orderLog.setTimestamp(new Timestamp(System.currentTimeMillis()));
        orderLog.setOrderid(orderid);
        orderLog.setLogdata(logdata);
        Session session=HibernateTools.getSession();
        session.save(orderLog);
//        HibernateTools.commit(session);
        return true;
    }
    public String buildlog(){
        Session session=HibernateTools.getSession();
        LoanBookLogRecord loanBookLogRecord=session.get(LoanBookLogRecord.class, this.getOrderid());
        UserinfoEntity userinfoEntity=session.get(UserinfoEntity.class, loanBookLogRecord.getUserId());
        if (userinfoEntity==null){
            userinfoEntity=new UserinfoEntity();
            userinfoEntity.setUsername("用户已删除原用户id:"+loanBookLogRecord.getUserId());
        }
        BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
        if (bookInfoEntity==null){
            bookInfoEntity = new BookInfoEntity();
            bookInfoEntity.setBook_name("图书已删除原图书id:"+loanBookLogRecord.getUserId());
        }
//        HibernateTools.commit(session);
        return TimeTools.timeStamp2Date(getTimestamp(),null)+"   " +userinfoEntity.getUsername()+"   " +getLogdata()+"   " +bookInfoEntity.getBook_name();
    }

    @Column(name = "id")
    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    @Basic
    @Column(name = "orderid")
    public int getOrderid() {
        return orderid;
    }

    public void setOrderid(int userid) {
        this.orderid = userid;
    }

    @Basic
    @Column(name = "timestamp")
    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }
    @Basic
    @Column(name = "logdata")
    public String getLogdata() {
        return logdata;
    }

    public void setLogdata(String logdata) {
        this.logdata = logdata;
    }
}
